﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RainEngine.SceneManagement;

namespace RainEngine.SceneNodes.UI
{
    /// <summary>
    /// 按钮控件基类，按钮、复选框、单选框都是从这个类继承的
    /// </summary>
    public abstract class UIButtonBase:UISceneNode
    {
        /// <summary>
        /// 控件上显示的文字
        /// </summary>
        protected string text;

        /// <summary>
        /// 按钮文字颜色
        /// </summary>
        protected Color textColor;

        /// <summary>
        /// 按钮文字高亮颜色
        /// </summary>
        protected Color textColorHighlight;

        /// <summary>
        /// 文字使用的字体
        /// </summary>
        protected SpriteFont font;        

        /// <summary>
        /// 控件普通状态的源矩形
        /// </summary>
        protected Rectangle rectSourceOrigin;

        /// <summary>
        /// 控件高亮状态的源矩形
        /// </summary>
        protected Rectangle rectSourceHighlight;

        /// <summary>
        /// 创建一个新UIButtonBase对象，内部方法        
        /// </summary>
        /// <param name="engine">引擎</param>
        /// <param name="setScene">此控件所属的scene</param>
        /// <param name="setPosition">2D屏幕位置</param>
        /// <param name="setTextureName">包含路径的背景纹理的名称</param>
        /// <param name="setSize">2D屏幕大小</param>
        /// <param name="setText">按钮上的文字</param>
        /// <param name="setFont">字体</param>
        /// <param name="setTextColor">普通状态的文字颜色</param>
        /// <param name="setTextColorHighlight">高亮状态的文字颜色</param>        
        /// <param name="setRectOrigin">普通状态图像源矩形</param>
        /// <param name="setRectHighlight">高亮状态的图像源矩形</param>        
        /// <param name="setRectClicked">鼠标点击状态的图像源矩形</param>        
        internal UIButtonBase(RainGE engine, Scene setScene, Vector2 setPosition, string setTextureName, Vector2 setSize, string setText, SpriteFont setFont, Color setTextColor, Color setTextColorHighlight, Rectangle setRectOrigin, Rectangle setRectHighlight,bool useResource)
            : base(engine, setScene, setPosition, setTextureName,useResource )
        {
            this.size = setSize;
            this.text = setText;
            this.font = setFont;            
            this.textColor = setTextColor;
            this.textColorHighlight = setTextColorHighlight;
            this.rectSourceOrigin = setRectOrigin;
            this.rectSourceHighlight = setRectHighlight;            
        }

        #region 属性

        /// <summary>
        /// 获取或设置按钮上的文字
        /// </summary>
        public string Text
        {
            get { return text; }
            set
            {
                text = value;
                OnTextChanged(EventArgs.Empty);
            }
        }

        /// <summary>
        /// 获取或设置文字原始颜色
        /// </summary>
        public Color TextColor
        {
            get { return textColor; }
            set { textColor = value; }
        }

        /// <summary>
        /// 获取或设置文字高亮颜色
        /// </summary>
        public Color TextColorHighlight
        {
            get { return textColorHighlight; }
            set { textColorHighlight = value; }
        }

        /// <summary>
        /// 获取或设置文字使用的字体
        /// </summary>
        public SpriteFont Font
        {
            get { return font; }
            set
            {
                font = value;
                OnFontChanged(EventArgs.Empty);
            }
        }        

        /// <summary>
        /// 获取或设置纹理高亮状态的源矩形
        /// </summary>
        public Rectangle RectSourceHighlight
        {
            get { return rectSourceHighlight; }
            set { rectSourceHighlight = value; }
        }        
        
        #endregion

        #region 事件
        /// <summary>
        /// 文字改变时引发的事件
        /// </summary>
        public event EventHandler TextChanged;

        /// <summary>
        ///  TextChanged事件的处理程序
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnTextChanged(EventArgs e) 
        {
            if (TextChanged != null)
                TextChanged(this, e);
        }

        /// <summary>
        /// 字体改变时引发的事件
        /// </summary>
        public event EventHandler FontChanged;

        /// <summary>
        ///  FontChanged事件的处理程序
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnFontChanged(EventArgs e)
        {
            if (FontChanged != null)
                FontChanged(this, e);
        }

        #endregion
    }
}
